import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:namewava/jc_rote_path.dart';
import 'package:namewava/pages/mine/widgets/jc_pop.dart';
import 'package:namewava/utils/jc_cache.dart';
import 'package:namewava/utils/jc_global.dart';
import 'package:namewava/utils/jc_privacy_protocol.dart';
import 'package:namewava/utils/jc_text_style.dart';
import 'package:namewava/utils/js_request.dart';

enum ItemType {
  none,
  text,
  copy,
  delectAccount,
  translate,
  privacyPolicy,
  protocol
}

class JcMineCommentItem extends StatelessWidget {
  const JcMineCommentItem(
      {super.key,
      required this.title,
      this.imageIcon,
      this.rightValue,
      required this.type,
      this.copyValue,
      this.isTranslate});
  final String title;
  final String? imageIcon;
  final String? rightValue;
  final ItemType type;
  final String? copyValue;
  final RxBool? isTranslate;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        height: 50.h,
        alignment: Alignment.center,
        padding: EdgeInsets.only(top: 5.h, left: 19.w, right: 15.w),
        margin: EdgeInsets.only(left: 20.w, right: 20.w, bottom: 20.w),
        decoration: BoxDecoration(
            color: const Color(0xFFF9F9F9),
            //color: Colors.red,
            borderRadius: BorderRadius.circular(23.r)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              title,
              style: JcTextStyle.octoberDevanagariRegularTextStyle(
                  15.sp, Colors.black),
            ),
            type == ItemType.text
                ? Text(rightValue ?? '',
                    style: JcTextStyle.octoberDevanagariRegularTextStyle(
                        15.sp, Colors.black))
                : type == ItemType.copy
                    ? Image.asset(imageIcon ?? '')
                    : type == ItemType.translate
                        ? Obx(() {
                            return CupertinoSwitch(
                              value: isTranslate?.value ?? false,
                              onChanged: (v) {
                                isTranslate?.value =
                                    !(isTranslate?.value ?? false);

                                JcCache.getInstance()
                                    .setData('isTranslate', isTranslate?.value);
                              },
                              activeColor: const Color.fromRGBO(0, 46, 111, 1),
                              trackColor: const Color(0xFFCCCCCC),
                            );
                          })
                        : SizedBox(
                            width: 10.w,
                          ),
          ],
        ),
      ),
      onTap: () {
        if (type == ItemType.copy) {
          ///复制
          Clipboard.setData(ClipboardData(text: copyValue ?? ''));
          EasyLoading.showSuccess('Success');
        } else if (type == ItemType.delectAccount) {
          ///删除账号
          Get.dialog(
              JcPop(
                  btnTitles: const ['Delete Account'],
                  cancelTitle: 'Cancel',
                  tapEvents: (index) {
                    JcGlobal.of(context).logReport({'page': 'deleteaccount'});
                    JsRequest.deleteRequest(success: (res) {
                      _deleteResult();
                    });
                  }),
              useSafeArea: false);
        } else if (type == ItemType.privacyPolicy) {
          JcPrivacyProtocol.launchPrivacyProtocol(1);
        } else if (type == ItemType.protocol) {
          JcPrivacyProtocol.launchPrivacyProtocol(0);
        }
      },
    );
  }

  _deleteResult() {
    var wavaNameRecord = JcCache.getInstance().get('wavaNameRecord');
    var wavaUserId = JcCache.getInstance().get('wavaUserId');
    if (wavaNameRecord != null) {
      List records = jsonDecode(wavaNameRecord);
      if (records.isNotEmpty) {
        List tmp = [];
        for (var element in records) {
          if (element['userId'] != wavaUserId) {
            tmp.add(element);
          }
          JcCache.getInstance().setData('wavaNameRecord', jsonEncode(tmp));
        }
      }
    }

    var myBwlRecord = JcCache.getInstance().get('myBwlRecord');
    if (myBwlRecord != null) {
      List bwlRecords = jsonDecode(myBwlRecord);
      if (bwlRecords.isNotEmpty) {
        List tmp = [];
        for (var element in bwlRecords) {
          if (element['userId'] != wavaUserId) {
            tmp.add(element);
          }
          JcCache.getInstance().setData('myBwlRecord', jsonEncode(tmp));
        }
      }
    }
    JcCache.getInstance().setData('wavaIsLogin', false);
    Get.back();
    Get.toNamed(JcRotePath.jcLogin);
  }
}
